package com.equipment.fkdcn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.equipment.fkdcn.pojo.EType;
import com.equipment.fkdcn.service.ETypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/etype")
public class ETypeController {
    @Autowired//注解自动注入
    @Qualifier("eTypeService")//装配bean
   private ETypeService eTypeService;

    @GetMapping("/query")
    @ResponseBody//不走jsp页面
   public String AllType(){//查询功能
        List<EType> eTypes = eTypeService.AllQueryEType();
        String etype = JSON.toJSONString(eTypes);
        return etype;
   }



   //删除功能
    @GetMapping("/del")
    @ResponseBody
    public  String delType(@RequestParam("id")String id){
        boolean flag = eTypeService.delType(Integer.parseInt(id));
        return  "{\"state\":"+flag+"}";
    }


    //添加功能
    @PostMapping("/addType")
    @ResponseBody
    public String addType(@RequestBody String data){
        System.out.println(data);
        JSONObject jsonObject = JSON.parseObject(data);
        String name = jsonObject.getString("name");
        boolean flag = eTypeService.addType(name);
        return  "{\"state\":"+flag+"}";
    }


    //修改类型
    @PostMapping("/upType")
    @ResponseBody
    public  String  upType(@RequestBody String data){
        System.out.println(data);
        JSONObject jsonObject = JSON.parseObject(data);
        EType eType = JSON.toJavaObject(jsonObject, EType.class);
        boolean b = eTypeService.upType(eType);
        return  "{\"state\":"+b+"}";
    }



    //模糊查询
    @GetMapping("/likeQuery")
    @ResponseBody
    public String likeQuery(@RequestParam("name")String name){

        List<EType> eTypes = eTypeService.likeQuery("%"+name+"%");
        String s = JSON.toJSONString(eTypes);
        return s;
    }




}
